package com.shockweb.common.serializable;

import java.io.*;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.shockweb.common.International;
import com.shockweb.common.utils.classmethod.GenericType;
/**
 * 数据穿行化类
 * @author: 彭明华
 * History:
 * 2017年5月15日  彭明华创建
 */
public class SerializableObject{

    /**
     * boolean转byte[]
     * @param b
     * @return byte[]
     */
    public static byte[] booleanToBytes(boolean b) {
        if (b) {
            return new byte[]{-1};
        } else {
            return new byte[]{0};
        }
    }
    
    /**
     * char转byte[]
     * @param c
     * @return byte[]
     */
    public static byte[] charToBytes(char c) {
        byte[] b = new byte[2];
        b[0] = (byte) ((c & 0xFF00) >> 8);
        b[1] = (byte) (c & 0xFF);
        return b;
    }
    
    /**
     * short转byte[]
     * @param i
     * @return byte[]
     */
    public static byte[] shortToBytes(short i) {
        byte[] result = new byte[2];
        result[0] = (byte) ((i >> 8) & 0xFF);
        result[1] = (byte) (i & 0xFF);
        return result;
    }


    /**
     * int转byte[]
     * @param i
     * @return byte[]
     */
    public static byte[] intToBytes(int i) {
        byte[] result = new byte[4];
        result[0] = (byte) ((i >> 24) & 0xFF);
        result[1] = (byte) ((i >> 16) & 0xFF);
        result[2] = (byte) ((i >> 8) & 0xFF);
        result[3] = (byte) (i & 0xFF);
        return result;
    }
    

    /**
     * long转byte[]
     * @param i
     * @return byte[]
     */
    public static byte[] longToBytes(long i) {
        byte[] result = new byte[8];
        result[0] = (byte) ((i >> 56) & 0xFF);
        result[1] = (byte) ((i >> 48) & 0xFF);
        result[2] = (byte) ((i >> 40) & 0xFF);
        result[3] = (byte) ((i >> 32) & 0xFF);
        result[4] = (byte) ((i >> 24) & 0xFF);
        result[5] = (byte) ((i >> 16) & 0xFF);
        result[6] = (byte) ((i >> 8) & 0xFF);
        result[7] = (byte) (i & 0xFF);
        return result;
    }
    
    /**
     * float转byte[]
     * @param d
     * @return byte[]
     */
	public static byte[] floatToBytes(float d){  
		byte writeBuffer[]= new byte[4];  
			int v = Float.floatToIntBits(d);  
	        writeBuffer[0] = (byte)(v >>> 24);  
	        writeBuffer[1] = (byte)(v >>> 16);  
	        writeBuffer[2] = (byte)(v >>>  8);  
	        writeBuffer[3] = (byte)(v >>>  0);  
	        return writeBuffer;  
	  
	}
	

	
	/**
	 * double转byte[]
	 * @param d
	 * @return byte[]
	 */
	public static byte[] doubleToBytes(double d){  
		byte writeBuffer[]= new byte[8];  
		long v = Double.doubleToLongBits(d);  
        writeBuffer[0] = (byte)(v >>> 56);  
        writeBuffer[1] = (byte)(v >>> 48);  
        writeBuffer[2] = (byte)(v >>> 40);  
        writeBuffer[3] = (byte)(v >>> 32);  
        writeBuffer[4] = (byte)(v >>> 24);  
        writeBuffer[5] = (byte)(v >>> 16);  
        writeBuffer[6] = (byte)(v >>>  8);  
        writeBuffer[7] = (byte)(v >>>  0);  
        return writeBuffer;
	  
	}
	

    /**
     * date转byte[]
     * @param date
     * @return byte[]
     */
    public static byte[] dateToBytes(Date date){
    	return longToBytes(date.getTime());
    }
    
    /**
     * time转byte[]
     * @param time
     * @return byte[]
     */
    public static byte[] timeToBytes(Time time){
    	return longToBytes(time.getTime());
    }
    
    /**
     * timestamp转byte[]
     * @param timestamp
     * @return byte[]
     */
    public static byte[] timestampToBytes(Timestamp timestamp){
    	return longToBytes(timestamp.getTime());
    }
    
    /**
     * byte[]转short
     * @param b
     * @param offset
     * @return byte[]
     */
    public static short bytesToShort(byte[] b, int offset) {
        short value = 0;
        value += ((short) b[offset] & 0xFF) << 8;
        value += (short) b[1 + offset] & 0xFF;
        return value;
    }

    /**
     * byte[]转short
     * @param b
     * @return short
     */
    public static short bytesToShort(byte[] b) {
        return bytesToShort(b, 0);
    }

    /**
     * byte[]转int
     * @param b
     * @param offset
     * @return int
     */
    public static int bytesToInt(byte[] b, int offset) {
        int value = 0;
        value = ((int) b[offset] & 0xFF) << 24;
        value += ((int) b[1 + offset] & 0xFF) << 16;
        value += ((int) b[2 + offset] & 0xFF) << 8;
        value += (int) b[3 + offset] & 0xFF;
        return value;
    }

    /**
     * byte[]转int
     * @param b
     * @return int
     */
    public static int bytesToInt(byte[] b) {
        return bytesToInt(b, 0);
    }

    /**
     * byte[]转boolean
     * @param b
     * @param offset
     * @return boolean
     */
    public static boolean bytesToBoolean(byte[] b, int offset) {
        if (b[offset] == 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * byte[]转boolean
     * @param b
     * @return boolean
     */
    public static boolean bytesToBoolean(byte[] b) {
        return bytesToBoolean(b, 0);
    }

    /**
     * byte[]转char
     * @param b
     * @param offset
     * @return char
     */
    public static char bytesToChar(byte[] b, int offset) {
        char value = 0;
        value += ((char) b[offset] & 0xFF) << 8;
        value += (char) b[1 + offset] & 0xFF;
        return value;
    }

    /**
     * byte[]转char
     * @param b
     * @return char
     */
    public static char bytesToChar(byte[] b) {
        return bytesToChar(b, 0);
    }
    
    /**
     * byte[]转long
     * @param b
     * @param offset
     * @return long
     */
    public static long bytesToLong(byte[] b, int offset) {
        long value = 0;
        value = ((long) b[offset] & 0xFF) << 56;
        value += ((long) b[1 + offset] & 0xFF) << 48;
        value += ((long) b[2 + offset] & 0xFF) << 40;
        value += ((long) b[3 + offset] & 0xFF) << 32;
        value += ((long) b[4 + offset] & 0xFF) << 24;
        value += ((long) b[5 + offset] & 0xFF) << 16;
        value += ((long) b[6 + offset] & 0xFF) << 8;
        value += (long) b[7 + offset] & 0xFF;
        return value;
    }

    /**
     * byte[]转long
     * @param b
     * @return long
     */
    public static long bytesToLong(byte[] b) {
        return bytesToLong(b, 0);
    }
    
	/**
	 * byte[]转float
	 * @param b
	 * @return float
	 */
	public static float bytesToFloat(byte[] b){ 
	     return bytesToFloat(b,0);
	}
	
	/**
	 * byte[]转float
	 * @param b
	 * @param offset
	 * @return float
	 */
	public static float bytesToFloat(byte[] b, int offset){ 
	     return Float.intBitsToFloat((((b[0 + offset] & 255) << 24) +  
                ((b[1 + offset] & 255) << 16) +  
                ((b[2 + offset] & 255) <<  8) +  
                ((b[3 + offset] & 255) <<  0))
	     );  
	}
	
	/**
	 * byte[]转double
	 * @param b
	 * @return double
	 */
	public static double bytesToDouble(byte[] b){ 
	     return bytesToDouble(b,0);
	}
	
	/**
	 * byte[]转double
	 * @param b
	 * @param offset
	 * @return double
	 */
    public static double bytesToDouble(byte[] b, int offset) {
	     return Double.longBitsToDouble((((long)b[0] << 56) +  
	                ((long)(b[1 + offset] & 255) << 48) +  
	                ((long)(b[2 + offset] & 255) << 40) +  
	                ((long)(b[3 + offset] & 255) << 32) +  
	                ((long)(b[4 + offset] & 255) << 24) +  
	                ((b[5 + offset] & 255) << 16) +  
	                ((b[6 + offset] & 255) <<  8) +  
	                ((b[7 + offset] & 255) <<  0))
	          ); 
    }
	
    /**
     * byte[]转Date
     * @param b
     * @param offset
     * @return Date
     */
    public static Date bytesToDate(byte[] b, int offset){
    	return new Date(bytesToLong(b,offset));
    }
    
    /**
     * byte[]转Date
     * @param b
     * @return Date
     */
    public static Date bytesToDate(byte[] b){
    	return new Date(bytesToLong(b,0));
    }
    
    /**
     * byte[]转Date
     * @param b
     * @param offset
     * @return java.sql.Date
     */
    public static java.sql.Date bytesToSqlDate(byte[] b, int offset){
    	return new java.sql.Date(bytesToLong(b,offset));
    }
    
    /**
     * byte[]转Date
     * @param b
     * @return java.sql.Date
     */
    public static java.sql.Date bytesToSqlDate(byte[] b){
    	return new java.sql.Date(bytesToLong(b,0));
    }
    
    /**
     * byte[]转Time
     * @param b
     * @param offset
     * @return Time
     */
    public static Time bytesToTime(byte[] b, int offset){
    	return new Time(bytesToLong(b,offset));
    }
    
    /**
     * byte[]转Time
     * @param b
     * @return Time
     */
    public static Time bytesToTime(byte[] b){
    	return new Time(bytesToLong(b,0));
    }
    
    /**
     * byte[]转Timestamp
     * @param b
     * @param offset
     * @return Timestamp
     */
    public static Timestamp bytesToTimestamp(byte[] b, int offset){
    	return new Timestamp(bytesToLong(b,offset));
    }
    
    /**
     * byte[]转Timestamp
     * @param b
     * @return Timestamp
     */
    public static Timestamp bytesToTimestamp(byte[] b){
    	return new Timestamp(bytesToLong(b,0));
    }
    
    /**
     * 从byte数组的index位置开始获取一个len长度的字符串
     *
     * @param b     目标byte数组
     * @param index 从index位置开始进行操作
     * @param len   要获取的字符串的长度
     * @param charsetName
     * @return String
     */
    public static String bytesToString(byte[] b, int index, int len,String charsetName) throws UnsupportedEncodingException {
        return new String(b,index,len, charsetName);
    }

    /**
     * 字符串转byte[]
     * @param s
     * @param charsetName
     * @return byte[]
     * @throws UnsupportedEncodingException
     */
    public static byte[] stringToBytes(String s,String charsetName) throws UnsupportedEncodingException {
        return s.getBytes(charsetName);
    }
    
    /**
     * 将数据装换成16进制字符
     * @param data 字符源
     * @return char[] 返回16进制字符
     */
    public static final char[] bytesToHex(byte[] data) {
    	if(data==null){
    		return null;
    	}
        // 把密文转换成十六进制的字符串形式
        int j = data.length;
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        char chars[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = data[i];
            chars[k++] = hexDigits[byte0 >>> 4 & 0xf];
            chars[k++] = hexDigits[byte0 & 0xf];
        }
        return chars;
    }
    
    /**
     * 将数据装换成16进制字符串
     * @param data 字符源
     * @return String 返回16进制字符串
     */
    public static final String bytesToHexString(byte[] data) {
    	return new String(bytesToHex(data));
    }
    
    /**
     * 16进制编码
     */
    private static char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    /**
     * 单个16进制转二进制
     * @param c 16进制字符
     * @return int 返回实际二进制
     */
    private static int hexToNum(char c) {
        int num = 0;
        for (int j = 0; j < hexDigits.length; j++) {
            if (c == hexDigits[j])
                num = j;
        }
        return num;
    }

    /**
     * 16进制转二进制
     * @param data 16进制字符
     * @return byte[] 返回实际二进制数据
     */
    public static byte[] hexToBytes(char[] data) {
    	if(data==null){
    		return null;
    	}
        byte[] result = new byte[data.length / 2];
        for (int i = 0; i < data.length - 1; i++) {
            if (i % 2 == 0) {
                result[i / 2] = (byte) (hexToNum((data[i])) * 16 + hexToNum(data[i + 1]));
            }
        }
        return result;
    }
    /**
     * 16进制转二进制
     * @param data
     * @return byte[]
     */
    public static byte[] hexToBytes(String data) {
    	if(data==null){
    		return null;
    	}
    	return hexToBytes(data.toCharArray());
    }


    /**
     * base64基本字符
     */
    private static char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray();

    /**
     * codes缓冲
     */
    private static byte[] codes = new byte[256];

    static {
        for (int i = 0; i < 256; i++) {
            codes[i] = -1;
            // LoggerUtil.debug(i + "&" + codes[i] + " ");
        }
        for (int i = 'A'; i <= 'Z'; i++) {
            codes[i] = (byte) (i - 'A');
            // LoggerUtil.debug(i + "&" + codes[i] + " ");
        }

        for (int i = 'a'; i <= 'z'; i++) {
            codes[i] = (byte) (26 + i - 'a');
            // LoggerUtil.debug(i + "&" + codes[i] + " ");
        }
        for (int i = '0'; i <= '9'; i++) {
            codes[i] = (byte) (52 + i - '0');
            // LoggerUtil.debug(i + "&" + codes[i] + " ");
        }
        codes['+'] = 62;
        codes['/'] = 63;
    }

    /**
     * 对byte[]进行base64编码
     * @param data
     * @return String 返回字符串
     */
    public static String base64EncodeString(byte[] data) {
    	if(data==null){
    		return null;
    	}
    	return new String(base64Encode(data));
    }
    
    /**
     * 对byte[]进行base64编码
     * @param data
     * @return char[] 返回字符
     */
    public static char[] base64Encode(byte[] data) {
    	if(data==null){
    		return null;
    	}
        char[] out = new char[((data.length + 2) / 3) * 4];
        for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
            boolean quad = false;
            boolean trip = false;
            int val = (0xFF & (int) data[i]);
            val <<= 8;
            if ((i + 1) < data.length) {
                val |= (0xFF & (int) data[i + 1]);
                trip = true;
            }
            val <<= 8;
            if ((i + 2) < data.length) {
                val |= (0xFF & (int) data[i + 2]);
                quad = true;
            }
            out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
            val >>= 6;
            out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
            val >>= 6;
            out[index + 1] = alphabet[val & 0x3F];
            val >>= 6;
            out[index + 0] = alphabet[val & 0x3F];
        }
        return out;
    }
    
    /**
     * 对String进行base64解码
     * @param data
     * @return byte[] 返回解码后的数据
     */
    public static byte[] base64Decode(String data) {
    	if(data==null){
    		return null;
    	}
    	return base64Decode(data.toCharArray());
    }

    /**
     * 对char[]进行base64解码
     * @param data 编码后的字符数组
     * @return byte[] 返回解码后的数据
     */
    public static byte[] base64Decode(char[] data) {
    	if(data==null){
    		return null;
    	}
        int tempLen = data.length;
        for (int ix = 0; ix < data.length; ix++) {
            if ((data[ix] > 255) || codes[data[ix]] < 0) {
                --tempLen; // ignore non-valid chars and padding
            }
        }
        // calculate required length:
        // -- 3 bytes for every 4 valid base64 chars
        // -- plus 2 bytes if there are 3 extra base64 chars,
        // or plus 1 byte if there are 2 extra.

        int len = (tempLen / 4) * 3;
        if ((tempLen % 4) == 3) {
            len += 2;
        }
        if ((tempLen % 4) == 2) {
            len += 1;

        }
        byte[] out = new byte[len];

        int shift = 0; // # of excess bits stored in accum
        int accum = 0; // excess bits
        int index = 0;

        // we now go through the entire array (NOT using the 'tempLen' value)
        for (int ix = 0; ix < data.length; ix++) {
            int value = (data[ix] > 255) ? -1 : codes[data[ix]];

            if (value >= 0) { // skip over non-code
                accum <<= 6; // bits shift up by 6 each time thru
                shift += 6; // loop, with new bits being put in
                accum |= value; // at the bottom.
                if (shift >= 8) { // whenever there are 8 or more shifted in,
                    shift -= 8; // write them out (from the top, leaving any
                    out[index++] = // excess at the bottom for next iteration.
                            (byte) ((accum >> shift) & 0xff);
                }
            }
        }

        // if there is STILL something wrong we just have to throw up now!
        if (index != out.length) {
            throw new Error("Miscalculated data length (wrote " + index
                    + " instead of " + out.length + ")");
        }

        return out;
    }
    

    
    /**
     * 返回source的子串
     * @param source
     * @param index
     * @param len
     * @return byte[]
     */
    public static byte[] copyOfRange(byte[] source,int index,int len){
    	byte[] rtn = new byte[len];
    	for(int i=0;i<source.length && i<len;i++){
    		rtn[i] = source[index + i];
    	}
    	return rtn;
    }
    
    /**
     * 字符串合并
     * @param bytes
     * @return byte[]
     */
    public static byte[] bytesMerge(byte[]... bytes){
    	int len = 0;
    	for(byte[] b:bytes){
    		if(b!=null && b.length>0){
    			len = len + b.length;
    		}
    	}
    	if(len>0){
    		byte[] rtn = new byte[len];
    		int index = 0;
        	for(byte[] b:bytes){
        		if(b!=null && b.length>0){
        			copyBytes(b, rtn, index);
        			index = index + b.length;
        		}
        	}
    		return rtn;
    	}else{
    		return null;
    	}
    }
    
    /**
     * 比较两个byte[]数据是否一致
     * @param source
     * @param tag
     * @return boolean
     */
    public static boolean equals(byte[] source,byte[] tag){
    	if(source==null && tag==null){
    		return true;
    	}else if(source==null && tag!=null){
    		return false;
    	}else if(source!=null && tag==null){
    		return false;
    	}else{
    		if(source.length!=tag.length){
    			return false;
    		}else if(source.length==0){
    			return true;
    		}else{
    			for(int i=0;i<source.length;i++){
    				if(source[i]!=tag[i]){
    					return false;
    				}
    			}
    			return true;
    		}
    	}
    }
    

	/**
	 * 比较两个对象数组
	 * @param src
	 * @param tag
	 * @return boolean
	 */
	public static boolean equals(Object[] src,Object[] tag){
		if(src==null){
			if(tag==null){
				return true;
			}else{
				return false;
			}
		}else{
			if(tag==null){
				return false;
			}else{
				if(tag.length!=src.length){
					return false;
				}else{
					for(int i=0;i<src.length;i++){
						if(!src[i].equals(tag[i])){
							return false;
						}
					}
					return true;
				}
			}
		}
	}
    
    /**
     * 将source复制到target的index位置
     * @param source
     * @param target
     * @param index
     */
    public static void copyBytes(byte[] source,byte[] target,int index){
    	int len = source.length<target.length-index?source.length:target.length-index;
    	for(int i=0;i<len;i++){
    		target[index + i] = source[i];
    	}
    }

    /**
     * 将source复制到target的index位置
     * @param source
     * @param target
     * @param index
     */
    public static void copyBytes(byte[] source,byte[] target,int index,int len){
    	int tmp = source.length<target.length-index?source.length:target.length-index;
    	tmp = tmp>len?len:tmp;
    	for(int i=0;i<tmp;i++){
    		target[index + i] = source[i];
    	}
    }

    
    /**
     * 深度克隆对象
     * @param obj
     * @return Object
     */
    public static Object clone(Object obj)
    {
        ByteArrayOutputStream bo = null;
        ObjectOutputStream oo = null;
        ByteArrayInputStream bi = null;
        ObjectInputStream oi = null;
        try
        {
            bo = new ByteArrayOutputStream();
            oo = new ObjectOutputStream(bo);
            oo.writeObject(obj);
            bi = new ByteArrayInputStream(bo.toByteArray());
            oi = new ObjectInputStream(bi);
            return oi.readObject();
        }
        catch(Exception e)
        {
            throw new InternalError(e.getMessage());
        }
        finally {
	        try
	        {
	            if(oi != null)
	                oi.close();
	        }
	        catch(Exception exception1) { }
	        try
	        {
	            if(bi != null)
	                bi.close();
	        }
	        catch(Exception exception2) { }
	        try
	        {
	            if(oo != null)
	                oo.close();
	        }
	        catch(Exception exception3) { }
	        try
	        {
	            if(bo != null)
	                bo.close();
	        }
	        catch(Exception exception4) { }
        }
    }
    
    /**
     * 将bytes转化成对象实例
     * @param bytes
     * @return Object
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Object byteToObject(byte[] bytes) throws IOException, ClassNotFoundException
    {
    	return byteToObject(bytes,0,bytes.length);
    }
    /**
     * 将bytes转化成对象实例
     * @param bytes
     * @param offset
     * @param length
     * @return Object
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Object byteToObject(byte[] bytes,int offset,int length) throws IOException, ClassNotFoundException
    {
        ByteArrayInputStream bi = null;
        ObjectInputStream oi = null;
        try{
            bi = new ByteArrayInputStream(bytes,offset,length);
            oi = new ObjectInputStream(bi);
            return oi.readObject();
        }finally{
	        if(oi != null)
	            try
	            {
	                oi.close();
	            }
	            catch(Exception exception1) { }
	        if(bi != null)
	            try
	            {
	                bi.close();
	            }
	            catch(Exception exception2) { }
        }
    }


    /**
     * 将对象实例转化成 byte[]
     * @param obj
     * @return byte[]
     * @throws IOException
     */
    public static byte[] objectToByte(Object obj)throws IOException
    {
    	if(obj==null){
    		return null;
    	}
        byte bytes[] = null;
        ByteArrayOutputStream bo = null;
        ObjectOutputStream oo = null;
        try{
            bo = new ByteArrayOutputStream();
            oo = new ObjectOutputStream(bo);
            oo.writeObject(obj);
            oo.flush();
            bo.flush();
            bytes = bo.toByteArray();
        }finally{
            if(oo != null)
                try
                {
                    oo.close();
                }
                catch(Exception exception1) { }
            if(bo != null)
                try
                {
                    bo.close();
                }
                catch(Exception exception2) { }
        }
        return bytes;
    }

    /**
     * 创建和实例化List对象
     * @param clazz
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
	@SuppressWarnings({"unchecked"})
	private  static List<Object> instanceList(Class<?> clazz) throws InstantiationException, IllegalAccessException{
		if(clazz.isInterface()){
			return new ArrayList<Object>();
		}else{
			return (List<Object>)clazz.newInstance();
		}
	}
	
	

	/**
	 * 将value对象转化成clazz的对象
	 * @param clazz 目标对象类型
	 * @param value 值
	 * @param generic list、map对象class类型描述
	 * @param genericIndex 
	 * @return Object
	 * @throws ParseException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public static Object objectConvert(Class<?> clazz,Object value,GenericType generic,int genericIndex)throws ParseException, InstantiationException, IllegalAccessException{
		if(value==null){
			return null;
		}else if(clazz.getName().equals(Object.class.getName())){
			if(value.getClass().isArray() && Array.getLength(value)==1){
				return Array.get(value, 0).toString();
			}else if(List.class.isAssignableFrom(value.getClass()) && ((List<?>)value).size()==1){
				return ((List<?>)value).get(0);
			}else{
				return value;
			}
		}else if(!List.class.isAssignableFrom(clazz) && clazz.isAssignableFrom(value.getClass())){
			return value;
		}else{
			if(clazz.isArray()){
				Class<?> clazz2 = clazz.getComponentType();
				if(value.getClass().isArray()){
					int len = Array.getLength(value);
					Object[] os = (Object[]) Array.newInstance(clazz2,len);
					if(clazz2.isArray() || List.class.isAssignableFrom(clazz2)){
						for(int i=0;i<len;i++){
							os[i] = objectConvert(clazz2,Array.get(value, i),generic,genericIndex);
						}
					}else{
						for(int i=0;i<len;i++){
							if(Array.get(value, i)!=null){
								os[i] = stringToObject(clazz2,Array.get(value, i).toString());
							}else{
								os[i] = null;
							}
						}
					}
					return os;
				}else if(List.class.isAssignableFrom(value.getClass())){
					List<?> list = (List<?>)value;
					Object os[] = (Object[])Array.newInstance(clazz2,list.size());
					if(clazz2.isArray() || List.class.isAssignableFrom(clazz2)){
						for(int i=0;i<list.size();i++){
							os[i] = objectConvert(clazz2,list.get(i),generic,genericIndex);
						}
					}else{
						for(int i=0;i<list.size();i++){
							if(list.get(i)!=null){
								os[i] = stringToObject(clazz2,list.get(i).toString());
							}else{
								os[i] = null;
							}
						}
					}
					return os;
				}else{
					Object os[] = (Object[])Array.newInstance(clazz2,1);
					if(clazz2.isArray() || List.class.isAssignableFrom(clazz2)){
						os[0] = objectConvert(clazz2,value,generic,genericIndex);
					}else{
						os[0] = stringToObject(clazz2,value.toString());
					}
					return os;
				}
			}else if(List.class.isAssignableFrom(clazz)){
				Class<?> clazz2 = String.class;
				if(generic!=null && generic.type()!=null && generic.type().length>genericIndex){
					clazz2 = generic.type()[genericIndex];
				}
				List<Object> os = instanceList(clazz);
				if(value.getClass().isArray()){
					int len = Array.getLength(value);
					
					if(clazz2.isArray() || List.class.isAssignableFrom(clazz2)){
						for(int i=0;i<len;i++){
							os.add(objectConvert(clazz2,Array.get(value, i),generic,genericIndex+1));
						}
					}else{
						for(int i=0;i<len;i++){
							if(Array.get(value, i)!=null){
								os.add(stringToObject(clazz2,Array.get(value, i).toString()));
							}else{
								os.add(null);
							}
						}
					}
					
				}else if(List.class.isAssignableFrom(value.getClass())){
					List<?> list = (List<?>)value;
					if(clazz2.isArray() || List.class.isAssignableFrom(clazz2)){
						for(int i=0;i<list.size();i++){
							os.add(objectConvert(clazz2,list.get(i),generic,genericIndex+1));
						}
					}else{
						for(int i=0;i<list.size();i++){
							if(list.get(i)!=null){
								os.add(stringToObject(clazz2,list.get(i).toString()));
							}else{
								os.add(null);
							}
						}
					}
				}else{
					if(clazz2.isArray() || List.class.isAssignableFrom(clazz2)){
						os.add(objectConvert(clazz2,value,generic,genericIndex+1));
					}else{
						os.add(stringToObject(clazz2,value.toString()));
					}
				}
				return os;
			}else{
				if(value.getClass().isArray()){
					if(Array.getLength(value)>0 && Array.get(value, 0)!=null){
						Object o = stringToObject(clazz,Array.get(value, 0).toString());
						if(o==null){
							return value;
						}else{
							return o;
						}
					}else{
						return null;
					}
				}else if(List.class.isAssignableFrom(value.getClass())){
					List<?> list = (List<?>)value;
					if(list.size()>0 && list.get(0)!=null){
						Object o = stringToObject(clazz,list.get(0).toString());
						if(o==null){
							return value;
						}else{
							return o;
						}
					}else{
						return null;
					}
				}else{
					Object o = stringToObject(clazz,value.toString());
					if(o==null){
						return value;
					}else{
						return o;
					}
				}
			}
		}
	}

	/**
	 * 将String数据转化成对象
	 * @param clazz
	 * @param object
	 * @return Object
	 * @throws ParseException 
	 */
	public static Object stringToObject(Class<?> clazz, String object) throws ParseException  {
		if(object==null){
			return null;
		}else if(clazz.equals(String.class)){
			return object;
		}else if(clazz.equals(BigInteger.class)){
			try{
				return new BigInteger(object);
			}catch(Exception e){
				return new BigInteger(object.replaceAll(",",""));
			}
		}else if(clazz.equals(BigDecimal.class)){
			try{
				return new BigDecimal(object);
			}catch(Exception e){
				return new BigDecimal(object.replaceAll(",",""));
			}
		}else if(clazz.equals(Integer.class)){
			DecimalFormat decParser = new DecimalFormat(International.IN_INTEGER);
			return decParser.parse(object).intValue();
		}else if(clazz.equals(int.class)){
			DecimalFormat decParser = new DecimalFormat(International.IN_INTEGER);
			return decParser.parse(object).intValue();
		}else if(clazz.equals(Long.class)){
			DecimalFormat decParser = new DecimalFormat(International.IN_INTEGER);
			return decParser.parse(object).longValue();
		}else if(clazz.equals(long.class)){
			DecimalFormat decParser = new DecimalFormat(International.IN_INTEGER);
			return decParser.parse(object).longValue();		
		}else if(clazz.equals(Double.class)){
			DecimalFormat decParser = new DecimalFormat(International.IN_DECIMAL);
			return decParser.parse(object).doubleValue();
		}else if(clazz.equals(double.class)){
			DecimalFormat decParser = new DecimalFormat(International.IN_DECIMAL);
			return decParser.parse(object).doubleValue();
		}else if(clazz.equals(Float.class)){
			DecimalFormat decParser = new DecimalFormat(International.IN_DECIMAL);
			return decParser.parse(object).floatValue();
		}else if(clazz.equals(float.class)){
			DecimalFormat decParser = new DecimalFormat(International.IN_DECIMAL);
			return decParser.parse(object).floatValue();
		}else if(clazz.equals(java.sql.Date.class)){
			try{
	            SimpleDateFormat sdf = new SimpleDateFormat(International.IN_DATETIME);
	            return new java.sql.Date(sdf.parse(object).getTime());
			}catch(ParseException e){
	            SimpleDateFormat sdf = new SimpleDateFormat(International.IN_DATE);
	            return new java.sql.Date(sdf.parse(object).getTime());
			}
		}else if(clazz.equals(Timestamp.class)){
			try{
				SimpleDateFormat sdf = new SimpleDateFormat(International.IN_DATETIMEMSEL);
				return new Timestamp(sdf.parse(object).getTime());
			}catch(ParseException e){
				try{
		            SimpleDateFormat sdf = new SimpleDateFormat(International.IN_DATETIME);
		            return new Timestamp(sdf.parse(object).getTime());
				}catch(ParseException e1){
		            SimpleDateFormat sdf = new SimpleDateFormat(International.IN_DATE);
		            return new Timestamp(sdf.parse(object).getTime());
				}
			}
		}else if(clazz.equals(Time.class)){
	        SimpleDateFormat sdf = new SimpleDateFormat(International.IN_TIME);
	        return new Time(sdf.parse(object).getTime());
		}else if(clazz.equals(Date.class)){
			try{
	            SimpleDateFormat sdf = new SimpleDateFormat(International.IN_DATETIME);
	            return sdf.parse(object);
			}catch(ParseException e){
	            SimpleDateFormat sdf = new SimpleDateFormat(International.IN_DATE);
	            return sdf.parse(object);
			}
		}else if(clazz.equals(Byte.class)){
			return new Byte(object);
		}else if(clazz.equals(byte.class)){
			return Byte.parseByte(object);
		}else if(clazz.equals(Short.class)){
			DecimalFormat decParser = new DecimalFormat(International.IN_INTEGER);
			return decParser.parse(object).shortValue();
		}else if(clazz.equals(short.class)){
			DecimalFormat decParser = new DecimalFormat(International.IN_INTEGER);
			return decParser.parse(object).shortValue();
		}else if(clazz.equals(Boolean.class)){
			return new Boolean(object);
		}else if(clazz.equals(boolean.class)){
			return Boolean.parseBoolean(object);
		}else if(clazz.equals(Character.class)){
			return new Character(object.toString().charAt(0));
		}else if(clazz.equals(char.class)){
			return object.toString().charAt(0);
		}else if(clazz.equals(StringBuilder.class)){
			return new StringBuilder(object.toString());
		}else if(clazz.equals(StringBuffer.class)){
			return new StringBuffer(object.toString());
		}else{
			return null;
		}
	}

	
}